package plantshop.view.listeners;
import java.awt.Point;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.MouseInputListener;

import plantshop.model.Player;
import plantshop.model.exceptions.ImproperFileTypeException;
import plantshop.view.MainMenu;
import plantshop.view.PlayerMenu;


public class MenuMouseListener implements MouseInputListener,Serializable {
	private static final long serialVersionUID = 7867311819728540671L;
	private MainMenu menu;
	private JFrame frame;
	private Map<String, BufferedImage> images;
	private Map<String, BufferedImage[]> animations;
	
	public MenuMouseListener(MainMenu menu, Map<String, BufferedImage> images, JFrame frame,  Map<String, BufferedImage[]> animations){
		this.menu = menu;
		this.images = images;
		this.frame = frame;
		this.animations = animations;
	}

	
	
	@Override
	public void mouseClicked(MouseEvent e) {		
	}

	@Override
	public void mousePressed(MouseEvent e) {
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		int[] coords = menu.getStartButtonCoords();
		Point p = e.getPoint();
		int xloc = p.x;
		int yloc = p.y;
		
		int xmin, ymin, xmax, ymax;
		
		for(int i=0; i < coords.length; i+=4){
			xmin = coords[i+0];
			ymin = coords[i+1];
			xmax = coords[i+0]+coords[i+2];
			ymax = coords[i+1]+coords[i+3];
			
			if((xloc <= xmax && xloc >= xmin) && (yloc >= ymin && yloc <= ymax)){
				if(i == 0){
					frame.getContentPane().removeAll();
					
					//Changed
					PlayerMenu pMenu = new PlayerMenu(false,frame,images, animations);
					frame.getContentPane().add(pMenu);
					List<KeyListener> kls = Arrays.asList(pMenu.getKeyListeners());
					for(KeyListener kl:kls){
						pMenu.removeKeyListener(kl);
					}
					pMenu.requestFocusInWindow();
					pMenu.addKeyListener(new NameListener(pMenu));
					
					
					frame.validate();
					frame.repaint();
				}else if(i == 4){
					frame.getContentPane().removeAll();
					
					PlayerMenu pMenu = new PlayerMenu(true,frame,images, animations);
					frame.getContentPane().add(pMenu);
					
					
					List<KeyListener> kls = Arrays.asList(pMenu.getKeyListeners());
					for(KeyListener kl:kls){
						pMenu.removeKeyListener(kl);
					}
					pMenu.requestFocusInWindow();
					pMenu.addKeyListener(new NameListener(pMenu));
					
					
					frame.validate();
					frame.repaint();

				}else if(i == 8){
					frame.getContentPane().removeAll();
					//Select which file the user should load
					JFileChooser fileDialog = new JFileChooser();
					int result = fileDialog.showOpenDialog(menu);
					
					if(result == JFileChooser.APPROVE_OPTION){
						//Load the file
						try{
							File file = fileDialog.getSelectedFile();
							String[] fileName = file.getPath().split("\\.");
							if(!fileName[1].equalsIgnoreCase("pla")){
								throw new ImproperFileTypeException();
							}
							FileInputStream fileIn = new FileInputStream(file);
							ObjectInputStream in = new ObjectInputStream(fileIn);
							//Read the file the way we want
							Player player = (Player)in.readObject();
							in.close();
							fileIn.close();
							new Player(player, images, frame, animations).startGame();
							
						}
						catch(ImproperFileTypeException ex){
							ex.printStackTrace();
							JOptionPane.showMessageDialog(menu, "The file you chose is not of the correct type, please try again", "File Type Error!", JOptionPane.ERROR_MESSAGE);
						}
						catch(IOException ex){
							ex.printStackTrace();
							JOptionPane.showMessageDialog(menu, "There was an error loading the file, please try again", "Loading Error!", JOptionPane.ERROR_MESSAGE);
						}
						catch (ClassNotFoundException ex) {
							ex.printStackTrace();
							JOptionPane.showMessageDialog(menu, "There was an error converting the file, please try again", "Conversion Error!", JOptionPane.ERROR_MESSAGE);
						}
												
					}
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {
	}

	@Override
	public void mouseDragged(MouseEvent e) {
	}

	@Override
	public void mouseMoved(MouseEvent e) {
	}

}
